﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using infoMngmntGUI.View;
using infoMngmntGUI.Control;
using System.Data;

namespace infoMngmntGUI.View
{
    /// <summary>
    /// Interaction logic for Search_Patient.xaml
    /// </summary>
    public partial class Search_Visit : IView
    {
        public Search_Visit()
        {
            minimumAccess = Control.AccessManagement.UserType.NURSE;
            InitializeComponent();
            patientSelectComboBox.ItemsSource = DbConnection.GetPatients();
        }

        public override string ToString()
        {
            return "Search For Visit";
        }

        private int GetPid()
        {
            if (patientSelectComboBox.SelectedIndex == -1)
                MessageBox.Show("Please Select a Patient",
                    "Invalid name", MessageBoxButton.OK, MessageBoxImage.Exclamation, MessageBoxResult.OK);

            string selectedPatientID = patientSelectComboBox.SelectedValue.ToString().Split('\t')[0];
            int pID = -1;
            int.TryParse(selectedPatientID, out pID);
            return pID;
        }

        private void patientSelectComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            dataGrid.HorizontalScrollBarVisibility = ScrollBarVisibility.Visible;
            dataGrid.VerticalScrollBarVisibility = ScrollBarVisibility.Visible;
            dataGrid.AutoGenerateColumns = true;

            DataSet dataSet = new DataSet("Query Data Set");
            DataTable queryResults = DbConnection.getVisits(GetPid());
            dataSet.Tables.Add(queryResults);
            dataGrid.ItemsSource = dataSet.Tables[0].DefaultView;
        }

        private void AddTest_Button_Click(object sender, RoutedEventArgs e)
        {
            
            int selectedIndex = dataGrid.SelectedIndex;
            if (selectedIndex == -1)
            {
                if (patientSelectComboBox.SelectedIndex == -1)
                    MessageBox.Show("Please Select a Visit",
                        "No Visit Selected", MessageBoxButton.OK, MessageBoxImage.Exclamation, MessageBoxResult.OK);
            }
            else
            {
                DataView dataSet = (DataView)dataGrid.ItemsSource;
                DataTable queryResults = dataSet.Table;
                int vID = (int)queryResults.Rows[selectedIndex][0];




                MainWindow.DisplayOrderTest(GetPid(), vID);
            }
        }
    }
}
